﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques;

public partial class Alertador : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            this.VisiblePanel = false;
            this.pnlAlertas.Visible = false;
            //if (HayAlertas())
            //{
            //    MostrarMensaje();
            //}
            this.lblMessage.Text = "Hay nuevas alertas generadas por sensores";
            this.linkVerAlertas.Text="Ver alertas";
            this.linkVerAlertas.PostBackUrl= "~/Modules/EnfermedadesControlEstanque/ConsultaAlarmas.aspx";
        }
        //this.ImageButton1.OnClientClick = "document.getElementById('" + pnlAlertas.ClientID + "').style.visibility='hidden'; return false;";
    }



    private Alerta UltimaAlerta
    {
        get { return (Alerta)this.ViewState["ultimaalerta"]; }
        set { ViewState["ultimaalerta"] = value; }
    }

    public bool VisiblePanel
    {
        get { return (bool) this.ViewState["visiblepanle"]; }
        set { this.ViewState["visiblepanle"] = value; }
    }

    private bool HayAlertas()
    {
        

        //Alerta.getManager.ClearFilters();
        //Alerta.getManager.ClearOrders();

        //DateTime fecha = DateTime.Now;

        //fecha = fecha.AddSeconds(-6);
        //Alerta.getManager.AddFilter("Fecha", BussinessLogicTecnoPez.GenericManager<Alerta>.filterOperator.BiggerThan, fecha);
        //Alerta.getManager.AddOrder("Fecha", BussinessLogicTecnoPez.GenericManager<Alerta>.Order.orderDirection.DESC);
        //List<Alerta> lista = Alerta.getManager.GetFilter();

        //if (lista.Count > 0)
        //{
        Alerta res;
        bool returned;
        returned= Alerta.HayAlerta(out res);

        this.UltimaAlerta = res;
        return returned;
    }

    private void MostrarMensaje()
    {
        if(this.VisiblePanel)
        {
            this.pnlAlertas.Visible = true;
            this.pnlAlertas.Style.Add("visibility","visible");
            this.lblEstanque.Text = this.UltimaAlerta.Estanque.ToString();
            this.lblDescripcion.Text = this.UltimaAlerta.Descripcion.ToString();
        }
        else
        {
            this.pnlAlertas.Visible = false;
            this.pnlAlertas.Style.Add("visibility", "hidden");
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (pnlAlertas.Visible == true)
        {
            //pnlAlertas.Visible = false;
        }
        else
        {
            if (HayAlertas())
            {
                this.VisiblePanel = true;
                
            }
        }
        MostrarMensaje();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        this.VisiblePanel = false;
        MostrarMensaje();
    }
}
